package com.tingyuge.base.structrue.node;

/**
 * @Created by xiaodao
 */
public class LinkedList<E> {

    private Node dummpHead;
    private int size;

    public LinkedList() {
        size = 0;
        dummpHead = new Node(null,null);
    }

    //获取链表中元素的个数
    public int getSize() {
        return size;
    }

    //是否为空

    public boolean isEmpty() {
        return size == 0;
    }



    /**
     * 插入中间的问题
     * @param e
     */
    public void add(E e,int index){
        if(index < 0 || index >size){
            throw  new IllegalArgumentException("add failed illegal index");
        }

            Node prev = dummpHead;

            //获取到要添加的前一个元素 因为有了dummpHead 所以不用index- 1
            for (int i = 0; i <index  ; i++) {
                prev = prev.next;
            }
//           Node node = new Node(e);
//            node.next =  prev.next  ;
//            prev.next=node;

            prev.next = new Node(e,prev.next);
            size++;




    }
    /**
     * 在链表头添加元素
     *
     * @param e
     */
    public void addFirst(E e) {
//        Node node = new Node(e);
//        node.next = head; //新的节点的next 指向原来的第一个节点
//        head = node;//新的节点变成第一个节点;
//        head = new Node(e,head);
//        size++;

        add(e,0);

    }

    public void addLast(E e){
        add(e,size);
    }

    /**
     * 私有类.
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }
    }

    /**
     * 获取链表的第i个元素
     * @return
     */
    public E get(int index){
        if(index <0  || index >=size){
            throw  new IllegalArgumentException("failed get  index incorrent");
        }
        Node cur = dummpHead.next;

        for (int i = 0; i <index ; i++) {
            cur = cur.next;
        }

        return cur.e;
    }

    public E getFirst(){return get(0);};

    public E getLast(){return get(size-1);};

    public void set(int index, E e){
        if(index <0  || index >=size){
            throw  new IllegalArgumentException("failed update  index incorrent");
        }
        Node cur = dummpHead.next;
        for (int i = 0; i <index ; i++) {
            cur = cur.next;
        }
        cur.e=e;

    }

    public boolean contains(E e ){
        Node cur =  dummpHead.next;
        while (cur!=null){
            if(cur.e .equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder();

        builder.append("linedList: top [ ");
        Node head = dummpHead.next;
        while (head != null){
            builder.append(head.e);
            head=head.next;
            if(head!=null){
                builder.append("->");
            }

        }

        builder.append("] last");

        return builder.toString();
    }
    //remove index and return index
    public E remove(int index){
        if(index <0  || index >=size){
            throw  new IllegalArgumentException("failed update  index incorrent");
        }
        Node prev =dummpHead;
        for (int i = 0; i <index ; i++) {
            prev = prev.next;

        }
        //要删除的元素
        Node restNext = prev.next;
        //1-2-3-4-5
        prev.next = restNext.next;
        restNext.next =null;
        size--;

        return restNext.e;
    }


    public E  removeFirst(){
       return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }


}
